<?php

namespace App\Http\Controllers\Car;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class AutoInsuranceController extends Controller
{
    use ResponseData;
    public function index(Request $request){
        if(!$request->filled('image')){
            return $this->failed('请传图片地址',201);
        }else{
            $image = $request->image;
        }
        $host = "http://autoinsur0.market.alicloudapi.com";
        $path = "/s/api/ocr/policy/autoInsurance";
        $method = "POST";
        $appcode = "c5d5c0e6a05b49f3b34da628a407b253";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
        $querys = "";
        $bodys = "imgUrl=".$image;
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);

        list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $curl ), 2 );

        $status = curl_getinfo( $curl );

        curl_close( $curl );
        info('contents'.json_encode($contents));
        $body = json_decode($contents, true);
        info('body'.json_encode($body));
        if($body){
            if(isset($body['data'])){
                return $this->success($body['data']);
            }
            return $this->failed($body);
        }else{
            return $this->failed($body);
        }

    }
//   新保单识别
    public function baodan(Request $request){
        if(!$request->filled('image')){
            return $this->failed('请传图片地址',201);
        }else{
            $image = $request->image;
        }
        $host = "http://ainsurance.market.alicloudapi.com";
        $path = "/s/api/ocr/policy/autoInsurance";
        $method = "POST";
        $appcode = "c5d5c0e6a05b49f3b34da628a407b253";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
        $querys = "";
        $bodys = "imgUrl=".$image;
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);

        list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $curl ), 2 );

        $status = curl_getinfo( $curl );

        curl_close( $curl );

        $body = json_decode($contents, true);
        if($body){
            if(isset($body['data'])){
                return $this->success($body['data']);
            }
            return $this->failed($body);
        }else{
            return $this->failed($body);
        }

    }

}
